package com.geeklan.smart.ui.settings;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.repository.user.AccountRepository;
import com.geeklan.smart.databinding.ActivityCancellationAccountBinding;
import com.geeklan.smart.ui.login.LoginActivity;
import com.geeklan.smart.ui.viewmodel.user.UserViewModel;
import com.geeklan.smart.ui.viewmodel.user.UserViewModelFactory;
import com.geeklan.smart.utils.SharedPreferencesUtil;

public class CancellationAccountActivity extends BaseActivity<ActivityCancellationAccountBinding> {
    private UserViewModel userViewModel;
    private boolean isCheck = false;
    @Override
    protected ActivityCancellationAccountBinding initBinding() {
        return ActivityCancellationAccountBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        userViewModel = new ViewModelProvider(this, new UserViewModelFactory(new AccountRepository())).get(UserViewModel.class);
        userViewModel.getResetAccountLiveData().observe(this, response -> {
            dismiss();
            finishAll();
            SharedPreferencesUtil.clearAll();
            startActivity(new Intent(this, LoginActivity.class));
        });

        userViewModel.getResetAccountErrorLiveData().observe(this, s -> {
            dismiss();
            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void initView() {
        SpannableStringBuilder builder = new SpannableStringBuilder(mBinding.textview2.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
        builder.setSpan(redSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mBinding.textview2.setText(builder);
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });
        mBinding.tvGiveUp.setOnClickListener(v -> {
            finish();
        });
        mBinding.tvConfirm.setOnClickListener(v -> {
            loading();
            userViewModel.removeUser();
        });

        mBinding.llAgree.setOnClickListener(v -> {
            if (isCheck) {
                isCheck = false;
                mBinding.ivCheck.setImageDrawable(ActivityCompat.getDrawable(this, R.drawable.ic_checkbox_def));
            } else {
                isCheck = true;
                mBinding.ivCheck.setImageDrawable(ActivityCompat.getDrawable(this, R.drawable.ic_checkbox_select));
            }
        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, CancellationAccountActivity.class);
        context.startActivity(intent);
    }
}